{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Cars example\n",
    "\n",
    "as [requested by zhangliye](https://discourse.julialang.org/t/simulate-v0-2-0-a-julia-package-for-discrete-event-simulation/31822/9).  Zhangliye pointed out that there is no explanation of how to implement shared resources in `DiscreteEvents.jl`. The [`SimPy example`](https://simpy.readthedocs.io/en/latest/simpy_intro/shared_resources.html) cited by him simulates five cars, sharing a battery charging station with two charging points.\n",
    "\n",
    "Resources can directly expressed in native Julia as tokens in a `Channel`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "using DiscreteEvents, Printf\n",
    "struct Cable end           # this stands for a resource"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Often in simulations resources hold some additional information. In those cases simply extend the struct."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "car (generic function with 1 method)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "function car(env::Clock, name::String, bcs::Channel, driving_time::Int, charge_duration::Int)\n",
    "    delay!(env, driving_time)\n",
    "    now!(env, SF(println, @sprintf(\"%s arriving at %d\", name, tau(env))))\n",
    "    cable = take!(bcs)                          # take the cable from the charging station\n",
    "    now!(env, SF(println, @sprintf(\"%s starting to charge at %d\", name, tau(env))))\n",
    "    delay!(env, charge_duration)\n",
    "    put!(bcs, cable)                            # put it back\n",
    "    now!(env, SF(println, @sprintf(\"%s leaving the bcs at %d\", name, tau(env))))\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that the `now!`-call transfers the print operation to the clock and that we have to `put!` back explicitly the resource to the bcs after having used it (the SymPy example uses a `with` expression). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Car 0 arriving at 0\n",
      "Car 0 starting to charge at 0\n",
      "Car 1 arriving at 2\n",
      "Car 1 starting to charge at 2\n",
      "Car 2 arriving at 4\n",
      "Car 0 leaving the bcs at 5\n",
      "Car 2 starting to charge at 5\n",
      "Car 3 arriving at 6\n",
      "Car 1 leaving the bcs at 7\n",
      "Car 3 starting to charge at 7\n",
      "Car 2 leaving the bcs at 10\n",
      "Car 3 leaving the bcs at 12\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\"run! finished with 20 clock events, 0 sample steps, simulation time: 20.0\""
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "env = Clock()\n",
    "bcs = Channel{Cable}(2)    # define the battery charging station\n",
    "for i in 1:2\n",
    "    put!(bcs, Cable())     # with two cables (resources)\n",
    "end\n",
    "for i in 0:3\n",
    "    process!(env, SP(i, car, env, \"Car $i\", bcs, i*2, 5), 1)\n",
    "end\n",
    "run!(env, 20)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Everything is pretty much the same as in the SimPy-example."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "@webio": {
   "lastCommId": null,
   "lastKernelId": null
  },
  "kernelspec": {
   "display_name": "Julia 1.3.1",
   "language": "julia",
   "name": "julia-1.3"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.3.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
